import os
import json
from transmission_simulator.structure.region import Region


class InfoLoader:
    def __init__(self, input_path='./input'):
        self.path_init = os.path.join(input_path, 'init.json')
        self.path_updates = os.path.join(input_path, 'updates.json')
        self.cur_updates = {}
        self.reimport_update_json()

    def get_init_info(self):
        with open(self.path_init, 'r', encoding="utf-8") as fi:
            init = json.load(fi)
        res_dict = {}
        for region_dict in init["regions"]:
            res_dict[region_dict["name"]] = Region(region_dict)
        return res_dict

    def get_cur_update(self, cur_day):
        return self.cur_updates.get(str(cur_day), [])

    def reimport_update_json(self):
        if os.path.exists(self.path_updates):
            try:
                with open(self.path_updates, 'r', encoding="utf-8") as fu:
                    self.cur_updates = json.load(fu)
            except IOError:
                return False
            return True
        else:
            return False
